package com.example.hfweather.ui.detail

import android.os.Handler
import android.os.Looper
import com.example.hfweather.base.BaseInteractor
import com.example.hfweather.util.WeatherUtils

import com.qweather.sdk.Callback
import com.qweather.sdk.QWeather
import com.qweather.sdk.basic.Indices
import com.qweather.sdk.basic.Lang
import com.qweather.sdk.basic.Unit
import com.qweather.sdk.parameter.indices.IndicesParameter
import com.qweather.sdk.parameter.weather.WeatherParameter
import com.qweather.sdk.response.error.ErrorResponse
import com.qweather.sdk.response.indices.IndicesDailyResponse
import com.qweather.sdk.response.weather.WeatherDailyResponse


class WeatherDetailInteractor : BaseInteractor<WeatherDetailView>() {

    /**
     * 获取3天天气回调
     */
    interface Get3DayWeatherCallBack {
        /**
         * 获取3天天气成功
         */
        fun get3DayWeatherSuccess(weather: WeatherDailyResponse)

        /**
         * 获取3天天气失败
         */
        fun get3DayWeatherFailure(error: String)
    }
    /**
     * 获取今天的生活指数回调
     */
    interface GetTodayIndicesCallBack {
        /**
         *  获取今天的生活指数成功
         */
        fun getTodayIndicesSuccess(weather: IndicesDailyResponse)

        /**
         *  获取今天的生活指数失败
         */
        fun getTodayIndicesFailure(error: String)
    }

    /**
     * 获取3天天气
     */
    fun get3DayWeather(latitude: String, longitude: String, callBack: Get3DayWeatherCallBack) {
        val tempParam = WeatherUtils.formatLatLng(longitude) + "," + WeatherUtils.formatLatLng(latitude)
        val parameter = WeatherParameter(tempParam)
            .lang(Lang.ZH_HANS)
            .unit(Unit.METRIC)

        QWeather.instance.weather3d(parameter, object : Callback<WeatherDailyResponse> {
            override fun onSuccess(response: WeatherDailyResponse) {
                Handler(Looper.getMainLooper()).post {
                    callBack.get3DayWeatherSuccess(response)
                }
            }

            override fun onFailure(errorResponse: ErrorResponse) {
                Handler(Looper.getMainLooper()).post {
                    callBack.get3DayWeatherFailure(errorResponse.toString())
                }
            }

            override fun onException(e: Throwable) {
                Handler(Looper.getMainLooper()).post {
                    callBack.get3DayWeatherFailure(e.toString())
                }
                e.printStackTrace()
            }
        })
    }
    /**
     * 获取当天生活指数
     */
    fun getTodayIndices(latitude: String, longitude: String, callBack: GetTodayIndicesCallBack) {
        val tempParam = WeatherUtils.formatLatLng(longitude) + "," + WeatherUtils.formatLatLng(latitude)
        val parameter = IndicesParameter(tempParam, Indices.ALL)
        QWeather.instance.indices1d(parameter, object : Callback<IndicesDailyResponse> {
            override fun onSuccess(response: IndicesDailyResponse) {
                Handler(Looper.getMainLooper()).post {
                    callBack.getTodayIndicesSuccess(response)
                }
            }

            override fun onFailure(errorResponse: ErrorResponse) {
                Handler(Looper.getMainLooper()).post {
                    callBack.getTodayIndicesFailure(errorResponse.toString())
                }
            }

            override fun onException(e: Throwable) {
                Handler(Looper.getMainLooper()).post {
                    callBack.getTodayIndicesFailure(e.toString())
                }
                e.printStackTrace()
            }
        })
    }
}